package tenantchannellogic

import (
	"context"
	model "gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/model/tenant/channel"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/pkg/xerrors"

	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type QrBankListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewQrBankListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QrBankListLogic {
	return &QrBankListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *QrBankListLogic) QrBankList(in *rpc.QrBankListRequest) (*rpc.QrBankListResponse, error) {
	var CommonResp rpc.CommonResp

	qrBankModel := model.NewTenantChannelQrBankModel(l.ctx, l.svcCtx.Db)
	qrBanks, err := qrBankModel.GetAllQrBank(in.BankName)
	if err != nil {
		CommonResp.Code = xerrors.DBError
		CommonResp.Message = "Database operation error"
		return &rpc.QrBankListResponse{
			CommonResp: &CommonResp,
		}, err
	}

	var records []*rpc.QrBank
	for _, qrBank := range qrBanks {
		records = append(records, &rpc.QrBank{
			Id:           qrBank.ID,
			CreatedAt:    qrBank.CreatedAt,
			UpdatedAt:    qrBank.UpdatedAt,
			MinAmount:    qrBank.MinAmount,
			MaxAmount:    qrBank.MaxAmount,
			DailyTimes:   qrBank.DailyTimes,
			DailyAmount:  qrBank.DailyAmount,
			RemainTimes:  qrBank.RemainTimes,
			RemainAmount: qrBank.RemainAmount,
			VisionLevel:  qrBank.VisionLevel,
			IsDisplay:    qrBank.IsDisplay,
			QrCode:       qrBank.QrCode,
			BankNickname: qrBank.BankNickname,
			BankName:     qrBank.BankName,
			Sort:         qrBank.Sort,
			Remark:       qrBank.Remark,
		})
	}

	return &rpc.QrBankListResponse{
		Records:    records,
		CommonResp: &CommonResp,
	}, nil
}
